<template>
  <div class="todo-footer" v-show="todoS.length">
    <!-- 没有待办事项时不展示 -->
    <label>
      <!-- <input type="checkbox" :checked="isAll" @click="checkAll" /> -->
      <input type="checkbox" v-model="isAll" />
    </label>
    <span>
      <span style="color: red">已完成{{ doneTotal }}</span> / 全部{{
        todoS.length
      }}
    </span>
    <button class="btn btn-danger" @click="clearFinish">清除已完成任务</button>
  </div>
</template>

<script>
export default {
  name: "FooterVue",
  props: ["todoS"],
  computed: {
    doneTotal: {
      get() {
        // return this.todoS.filter((item) => item.done).length;
        //filter和reduce的区别，filter会增加机器的工作量，filter的每一个返回的对象都是一个新数组，而reduce不会
        return this.todoS.reduce((pre, cur) => {
          /* if (cur.done) {
            return ++pre; //pre++有问题，++pre可以或者pre+1
          } else {
            return pre;
          } */
          return cur.done ? pre + 1 : pre;
          // return pre + cur.done;
        }, 0);
      },
    },
    // isAll() {
    //   return this.doneTotal === this.todoS.length && this.todoS.length;
    // },
    isAll: {
      get() {
        return this.doneTotal === this.todoS.length && this.todoS.length;
      },
      set(value) {
        this.$emit("checkAllTodo", value);
      },
    },
  },

  // methods: {
  //   checkAll(e) {
  //     this.checkAllTodo(e.target.checked);
  //   },
  // },

  methods: {
    clearFinish() {
      if (confirm("确定清除吗？？？")) {
        this.$emit("clearFinishTodo");
      }
    },
  },
};
</script>

<style scoped>
/*footer*/
.todo-footer {
  height: 40px;
  line-height: 40px;
  padding-left: 6px;
  margin-top: 5px;
}

.todo-footer label {
  display: inline-block;
  margin-right: 20px;
  cursor: pointer;
}

.todo-footer label input {
  position: relative;
  top: -1px;
  vertical-align: middle;
  margin-right: 5px;
}

.todo-footer button {
  float: right;
  margin-top: 5px;
}
</style>